<template>
    <div>
        <el-table :data="rolelist" border style="width: 100%">
            <el-table-column prop="id" label="角色编号" />
            <el-table-column prop="rolename" label="角色名称" />
            <el-table-column label="状态">
                <template #default="item">
                    <div>
                        <el-tag type="success" v-if="item.row.status == 1">启用</el-tag>
                        <el-tag type="danger" v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>>
            <el-table-column label="操作">
                <template #default="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { defineEmits, computed } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import { useStore } from 'vuex';
import { roleDel } from '@/request/api';
const store = useStore()


const rolelist = computed(() => store.getters['role/getRoleList'])
// console.log(rolelist, 'rolelist');
const getRole = () => {
    store.dispatch('role/RoleListActions')
}
getRole()

let emit = defineEmits(['edit'])
let edit = (id: number) => {
    emit('edit', id)
}

const del = (id: number) => {
    ElMessageBox.confirm(
        '你确定要删除这条数据吗?',
        '提示警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            roleDel({
                id
            }).then(res => {
                console.log(res, '角色删除');
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: res.msg,
                    })
                    getRole()
                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}
</script>

<style scope>
</style>
